package org.gwt.pluginframework.common.event;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.gwt.pluginframework.common.IPlugin;

/**
 * Base implementation of IPluginEvent. 
 * 
 * There should not be more implementations because the class is simple
 * and through parameters generic.
 * 
 * @author mszymanski
 */
public class PluginEvent implements IPluginEvent, Serializable {
	private static final long serialVersionUID = -362848771814533765L;

	protected String name;
	
	protected IPlugin invoker;
	
	protected Map<String, Object> parameters = new HashMap<String, Object>();
	
	public PluginEvent(String name) {
		this.name = name;
	}
	
	public PluginEvent(String name, String[] keys, Object[] values) {
		this.name = name;
		if(keys.length != values.length) {
			throw new IllegalArgumentException("Keys and values tables are not equal size!");
		}
		for(int i = 0; i < keys.length; i++) {
			parameters.put(keys[i], values[i]);
		}
	}
	
	public PluginEvent(String name, IPlugin invoker) {
		this.name = name;
		this.invoker = invoker;
	}
	
	public PluginEvent(String name, IPlugin invoker, Map<String, Object> params) {
		this.name = name;
		this.invoker = invoker;
		parameters.putAll(params);
	}
	
	public PluginEvent(String name, IPlugin invoker, String[] keys, Object[] values) {
		this.name = name;
		this.invoker = invoker;
		if(keys.length != values.length) {
			throw new IllegalArgumentException("Keys and values tables are not equal size!");
		}
		for(int i = 0; i < keys.length; i++) {
			parameters.put(keys[i], values[i]);
		}
	}

	/* (non-Javadoc)
	 * @see org.gwt.pluginframework.common.event.IPluginEvent#getParameters()
	 */
	public Map<String, Object> getParameters() {
		return parameters;
	}

	/* (non-Javadoc)
	 * @see org.gwt.pluginframework.common.event.IPluginEvent#getInvoker()
	 */
	public IPlugin getInvoker() {
		return invoker;
	}
	
	public void setInvoker(IPlugin invoker) {
		this.invoker = invoker;
	}

	/* (non-Javadoc)
	 * @see org.gwt.pluginframework.common.event.IPluginEvent#getName()
	 */
	public String getName() {
		return name;
	}
}
